package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.*;
import com.itheima.service.ClazzService;
import com.itheima.service.StudentService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private ClazzService clazzService;

    @Autowired
    private StudentMapper studentMapper;
    @GetMapping
    public Result list(StudentQueryParam student) {
        log.info("查询所有学生信息：{}", student);

        Page<Student> page = new Page<>(student.getPage(), student.getPageSize());

        //条件查询
        LambdaUpdateWrapper<Student> luw = new LambdaUpdateWrapper<>();
        luw.like(student.getName()!=null, Student::getName,student.getName());
        luw.like(student.getDegree()!=null, Student::getDegree,student.getDegree());
        luw.like(student.getClazzId()!=null, Student::getClazzId,student.getClazzId());

        studentService.page(page,luw);

        PageResult<Student> students = new PageResult<>(page.getTotal(),page.getRecords());

        //获取班级信息
        List<Clazz> clazzes = clazzService.list();
        if (clazzes!=null&&clazzes.size()>0){
            //获取学生信息
            List<Student> studentList = page.getRecords();
            for (Clazz clazz : clazzes) {
                for (Student student1 : studentList) {
                    if (student1.getClazzId()!=null&&student1!=null&&student1.getClazzId().equals(clazz.getId())){
                        student1.setClazzName(clazz.getName());
                    }
                }
            }
        }

        return Result.success(students);
    }

    @PostMapping
    @Transactional(rollbackFor = Exception.class)
    public Result save(@RequestBody Student student) {
        log.info("新增学生信息：{}", student);

        studentService.save(student);
        return Result.success();
    }

    @Transactional(rollbackFor = Exception.class)
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据id查询学生信息：{}", id);

        Student student = studentService.getById(id);
        return Result.success(student);
    }

    @PutMapping
    @Transactional(rollbackFor = Exception.class)

    public Result update(@RequestBody Student student) {
        log.info("修改学生信息：{}", student);

        studentService.updateById(student);
        return Result.success();
    }

    @DeleteMapping("/{ids}")
    @Transactional(rollbackFor = Exception.class)

    public Result delete(@PathVariable List<Integer> ids) {
        log.info("根据id删除学生信息：{}", ids);

        studentService.removeBatchByIds(ids);
        return Result.success();
    }

    @PutMapping("/violation/{id}/{score}")
    @Transactional(rollbackFor = Exception.class)

    public Result violation(@PathVariable Integer id, @PathVariable Integer score) {
        log.info("根据id{}扣分, 扣分：{}", id, score);

        LambdaUpdateWrapper<Student> luw = new LambdaUpdateWrapper<>();
        luw.ge(Student::getId,id);
        luw.setSql("violation_count = violation_count + {0}",1);
        luw.setSql(score!=null,"violation_score =violation_score+{0}",score);

        studentMapper.update(luw);
        return Result.success();
    }
}
